home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1405 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Inline function problem
  5. Date: 10 Jan 1996 21:17:03 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan10161703@g7240065.bridge.bst.bls.com>
  8. References: <4d10i5$bc3@newsroom.hitc.com>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: Chris Ruegger's message of 10 Jan 1996 18:29:25 GMT
  11.  
  12. In article <4d10i5$bc3@newsroom.hitc.com> Chris Ruegger <cruegger@eos.hitc.com> writes:
  13.  
  14. : I thought I was able to do the following but I am getting an
  15. : unresolved reference:
  16.  
  17. : (Running SunOS 5.4)
  18.  
  19. : // in file foo.H
  20. : class FOO {
  21. : public:
  22. :   int x();
  23. : private:
  24. :   int x_;
  25. : };
  26.  
  27. : // in file foo.C:
  28. : #include <foo.H>
  29. : inline int FOO::x() {return x_;}
  30.  
  31. This needs to be in the header file !!
  32.  
  33. : // main.C
  34. : #include <foo.H>
  35. : #include <iostream.h>
  36. : int main(int argc, char *argv[])
  37. : {
  38. :   FOO myfoo;
  39. :   cout << myfoo.x() << endl;
  40. : }
  41.  
  42. : When I try to link, I get:
  43.  
  44. : Undefined                       first referenced
  45. :  symbol                             in file
  46. : FOO::x(void)                         main.o
  47. : ld: fatal: Symbol referencing errors. No output written to main
  48.  
  49. : }
  50.  
  51. : Is this a compiler specific thing or illegal C++?
  52.  
  53. This is illegal C++
  54.  
  55. 7.1.2 Function specifiers
  56. ...
  57. 3  An inline function shall be defined in every transalation unit in which 
  58.    it is used ... If a function with external linkage is declared inline in one
  59.    translation unit, it shall be declared in line in all translation units in
  60.    which it appears.
  61.  
  62. Putting the inline function in the header file will guarentee this.
  63.  
  64. Regards
  65.  
  66.   -A.
  67. -- 
  68. | A.Champion                |
  69.